using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;


namespace SIS.DAL {
/// <summary>
/// The TimeScheduleTableDB class is responsible for interacting with the database to retrieve and store information
/// about TimeScheduleTable objects.
/// </summary>
public class TimeScheduleTableDB

{
#region "Public Methods"

/// <summary>
/// Gets an instance of TimeScheduleTable from the underlying datasource.
/// </summary>
/// <param name="timeTableKey">The unique TimeTableKey of the TimeScheduleTable in the database.</param>
/// <returns>An TimeScheduleTable when the TimeTableKey was found in the database, or null otherwise.</returns>
public static TimeScheduleTable GetItem(int timeTableKey)
{
TimeScheduleTable myTimeScheduleTable = null;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocTimeScheduleTableSelectSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@timeTableKey", timeTableKey);

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.Read()) {
myTimeScheduleTable = FillDataRecord(myReader);
}
myReader.Close();
}
myConnection.Close();
}
return myTimeScheduleTable;
}
}

/// <summary>
/// Returns a list with TimeScheduleTable objects.
/// </summary>
/// <returns>A generics List with the TimeScheduleTable objects.</returns>
public static TimeScheduleTableList GetList()
{
TimeScheduleTableList tempList = new TimeScheduleTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocTimeScheduleTableSelectList", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
}
return tempList;
}

/// <summary>
/// Returns a list with TimeScheduleTable objects.
/// </summary>
/// <returns>A generics List with the TimeScheduleTable objects.</returns>
public static TimeScheduleTableList GetListByClassRoomLocationKey(int ClassRoomLocationKey)
{
TimeScheduleTableList tempList = new TimeScheduleTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocTimeScheduleTableSelectListByClassRoomLocationKey", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@classRoomLocationKey", ClassRoomLocationKey);
myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
return tempList;
}
}

/// <summary>
/// Returns a list with TimeScheduleTable objects.
/// </summary>
/// <returns>A generics List with the TimeScheduleTable objects.</returns>
public static TimeScheduleTableList GetListByCourseDetailKey(int CourseDetailKey)
{
TimeScheduleTableList tempList = new TimeScheduleTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocTimeScheduleTableSelectListByCourseDetailKey", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@courseDetailKey", CourseDetailKey);
myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
return tempList;
}
}

/// <summary>
/// Returns a list with TimeScheduleTable objects.
/// </summary>
/// <returns>A generics List with the TimeScheduleTable objects.</returns>
public static TimeScheduleTableList GetListByDayCode(short DayCode)
{
TimeScheduleTableList tempList = new TimeScheduleTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocTimeScheduleTableSelectListByDayCode", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@dayCode", DayCode);
myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
return tempList;
}
}

/// <summary>
/// Returns a list with TimeScheduleTable objects.
/// </summary>
/// <returns>A generics List with the TimeScheduleTable objects.</returns>
public static TimeScheduleTableList GetListByEducationPeriodDetailKey(int EducationPeriodDetailKey)
{
TimeScheduleTableList tempList = new TimeScheduleTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocTimeScheduleTableSelectListByEducationPeriodDetailKey", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@educationPeriodDetailKey", EducationPeriodDetailKey);
myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
return tempList;
}
}

/// <summary>
/// Returns a list with TimeScheduleTable objects.
/// </summary>
/// <returns>A generics List with the TimeScheduleTable objects.</returns>
public static TimeScheduleTableList GetListByTeacherProfileKey(int TeacherProfileKey)
{
TimeScheduleTableList tempList = new TimeScheduleTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocTimeScheduleTableSelectListByTeacherProfileKey", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@teacherProfileKey", TeacherProfileKey);
myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
return tempList;
}
}

/// <summary>
/// Saves a TimeScheduleTable in the database.
/// </summary>
/// <param name="myTimeScheduleTable">The TimeScheduleTable instance to save.</param>
/// <returns>The new TimeTableKey if the TimeScheduleTable is new in the database or the existing TimeTableKey when an item was updated.</returns>
public static int Save(TimeScheduleTable myTimeScheduleTable)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocTimeScheduleTableInsertUpdateSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

if (myTimeScheduleTable.TimeTableKey == -1)
{myCommand.Parameters.AddWithValue("@timeTableKey", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@timeTableKey", myTimeScheduleTable.TimeTableKey);
}
if (myTimeScheduleTable.EducationPeriodDetailKey == null){
myCommand.Parameters.AddWithValue("@educationPeriodDetailKey", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@educationPeriodDetailKey", myTimeScheduleTable.EducationPeriodDetailKey);
}
if (myTimeScheduleTable.ClassRoomLocationKey == null){
myCommand.Parameters.AddWithValue("@classRoomLocationKey", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@classRoomLocationKey", myTimeScheduleTable.ClassRoomLocationKey);
}
if (myTimeScheduleTable.CourseDetailKey == null){
myCommand.Parameters.AddWithValue("@courseDetailKey", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@courseDetailKey", myTimeScheduleTable.CourseDetailKey);
}
if (myTimeScheduleTable.TeacherProfileKey == null){
myCommand.Parameters.AddWithValue("@teacherProfileKey", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@teacherProfileKey", myTimeScheduleTable.TeacherProfileKey);
}
if (myTimeScheduleTable.DayCode == null){
myCommand.Parameters.AddWithValue("@dayCode", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@dayCode", myTimeScheduleTable.DayCode);
}
if (myTimeScheduleTable.StartTime == null){
myCommand.Parameters.AddWithValue("@startTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@startTime", myTimeScheduleTable.StartTime);
}
if (myTimeScheduleTable.EndTime == null){
myCommand.Parameters.AddWithValue("@endTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@endTime", myTimeScheduleTable.EndTime);
}
if (myTimeScheduleTable.Status == null){
myCommand.Parameters.AddWithValue("@status", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@status", myTimeScheduleTable.Status);
}
if (myTimeScheduleTable.CreatedDateTime == null){
myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@createdDateTime", myTimeScheduleTable.CreatedDateTime);
}
if (myTimeScheduleTable.ModifiedDateTime == null){
myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@modifiedDateTime", myTimeScheduleTable.ModifiedDateTime);
}

DbParameter returnValue;
returnValue = myCommand.CreateParameter();
returnValue.Direction = ParameterDirection.ReturnValue;
myCommand.Parameters.Add(returnValue);

myConnection.Open();
myCommand.ExecuteNonQuery();
result = Convert.ToInt32(returnValue.Value);
myConnection.Close();
}
}
return result;
}

/// <summary>
/// Deletes a TimeScheduleTable from the database.
/// </summary>
/// <param name="timeTableKey">The TimeTableKey of the TimeScheduleTable to delete.</param>
/// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
public static bool Delete(int timeTableKey)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocTimeScheduleTableDeleteSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myCommand.Parameters.AddWithValue("@timeTableKey", timeTableKey);
myConnection.Open();
result = myCommand.ExecuteNonQuery();
myConnection.Close();
}
}
return result > 0;
}

#endregion

/// <summary>
/// Initializes a new instance of the TimeScheduleTable class and fills it with the data fom the IDataRecord.
/// </summary>
private static TimeScheduleTable FillDataRecord(IDataRecord myDataRecord )
{
TimeScheduleTable myTimeScheduleTable = new TimeScheduleTable();
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("TimeTableKey")))
{
myTimeScheduleTable.TimeTableKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("TimeTableKey"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("EducationPeriodDetailKey")))
{
myTimeScheduleTable.EducationPeriodDetailKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("EducationPeriodDetailKey"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ClassRoomLocationKey")))
{
myTimeScheduleTable.ClassRoomLocationKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("ClassRoomLocationKey"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CourseDetailKey")))
{
myTimeScheduleTable.CourseDetailKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("CourseDetailKey"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("TeacherProfileKey")))
{
myTimeScheduleTable.TeacherProfileKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("TeacherProfileKey"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("DayCode")))
{
myTimeScheduleTable.DayCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("DayCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("StartTime")))
{
myTimeScheduleTable.StartTime = myDataRecord.GetInt16(myDataRecord.GetOrdinal("StartTime"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("EndTime")))
{
myTimeScheduleTable.EndTime = myDataRecord.GetInt16(myDataRecord.GetOrdinal("EndTime"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
{
myTimeScheduleTable.Status = myDataRecord.GetInt16(myDataRecord.GetOrdinal("Status"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
{
myTimeScheduleTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
{
myTimeScheduleTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
}
return myTimeScheduleTable;
}
}

 } 
